package Day2025_03_17;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LeetCode446 {
    public int numberOfArithmeticSlices(int[] nums) {
        int n = nums.length;
        int[][] dp = new int[n][n];
        Map<Long, List<Integer>> hash = new HashMap<>();
        for (int i = 0; i < n; i++) {
            long tmp = (long) nums[i];
            if (!hash.containsKey(tmp)) hash.put(tmp, new ArrayList<Integer>());
            hash.get(tmp).add(i);
        }
        int sum = 0;
        for (int i = 2; i < n; i++) {
            for (int j = 1; j < i; j++) {
                long tmp = 2L * nums[j] - nums[i];
                if (hash.containsKey(tmp)) {
                    for (int x : hash.get(tmp)) {
                        if (x < j) dp[j][i] += dp[x][j] + 1;
                        else break;

                    }
                }
                sum += dp[j][i];
            }
        }
        return sum;
    }

    public static void main(String[] args) {
        LeetCode446 leetCode446 = new LeetCode446();
        System.out.println(leetCode446.numberOfArithmeticSlices(new int[]{2, 4, 6, 8, 10}));
    }
}
